<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML>
<html>
<head>
    <%@ include file="../../static/_head.jsp" %>
    <title>角色配置</title>
</head>
<body>
<article class="page-container">

<div class="col-xs-2"></div>
<div class="col-xs-6">
    <c:forEach items="${authPermissionModels}" var="rp">
        <table>
            <tr>
                <td class="c-primary lh-30">
                    <input type="checkbox" id="${rp.id}" value="${rp.id}|${rp.name}" name="${rp.id}" onchange="menuChecked(this,'${rp.id}')" <c:if test="${rp.isRole==1}">checked</c:if>>
                    &nbsp;导航名称：${rp.name}&nbsp;导航代码：${rp.code}</td>
            </tr>
            <c:if test="${not empty rp.authPermissionModels}">
                <c:forEach items="${rp.authPermissionModels}" var="rp_menu">
                    <tr>
                        <td class="c-secondary lh-30">&nbsp;&nbsp;&nbsp;&nbsp;
                            <input type="checkbox" id="${rp.id}|${rp_menu.id}" value="${rp_menu.id}|${rp_menu.name}" name="${rp.id}" onchange="parentChecked(this,'${rp.id}')" <c:if test="${rp_menu.isRole==1}">checked</c:if>>
                            &nbsp;菜单名称：${rp_menu.name}&nbsp;菜单代码：${rp_menu.code}</td>
                    </tr>
                </c:forEach>
            </c:if>
        </table>
    </c:forEach>
    <br/>
    <p><a href="javascript:void(0);" onclick="isRoleChecked('${authRole.id}')" class="btn btn-primary radius">更新</a></p>
</div>
</article>
<%@ include file="../../static/_footer.jsp" %>
<script>

    function isRoleChecked(roleId) {
        var chk_id = [], chk_name = [];
        $('input[type="checkbox"]:checked').each(function () {
            var temp = $(this).val().split("|");
            chk_id.push(temp[0]);
            chk_name.push(temp[1]);
        });
        console.log(chk_id);
        if (chk_id.length == 0) {
            layer.confirm('是否删除所有权限', {
                btn: ['确定', '取消']
            }, function () {
                $.ajax({
                    url: "/user/role/permission/index",
                    method: "POST",
                    data: "chkids=" + "[]&roleId="+roleId,
                    success: function (data) {
                        if (data.code == 200) {
                            var index = parent.layer.getFrameIndex(window.name);
                            layer.msg("数据保存中...",{time: 2000},function () {
                                parent.location.reload();
                                parent.layer.close(index);
                            });
                        } else {
                            var index = parent.layer.getFrameIndex(window.name);
                            layer.msg("数据更新失败...",{time: 2000},function () {
                                parent.location.reload();
                                parent.layer.close(index);
                            });
                        }
                    }
                });
            });
        } else {
            layer.confirm('是否更新角色权限为【' + chk_name + "】的数据", {
                btn: ['确定', '取消']
            }, function () {
                $.ajax({
                    method: "POST",
                    data: "chkids=" + chk_id+"&roleId="+roleId,
                    success: function (data) {
                        if (data.code == 200) {
                            var index = parent.layer.getFrameIndex(window.name);
                            layer.msg("数据保存中...",{time: 2000},function () {
                                parent.location.reload();
                                parent.layer.close(index);
                            });
                        } else {
                            var index = parent.layer.getFrameIndex(window.name);
                            layer.msg("数据更新失败...",{time: 2000},function () {
                                parent.location.reload();
                                parent.layer.close(index);
                            });
                        }
                    }
                });
            });
        }

    }

    function menuChecked(obj, rolePermissionId) {
        var parent = document.getElementById(rolePermissionId);
        if (!parent.checked) {
            var cks = document.getElementsByName(rolePermissionId);
            for ( var i = 0; i < cks.length; i++) {
                cks[i].checked = false;
            }
        }
    }

    function parentChecked(obj, rolePermissionId) {
        $("#" + rolePermissionId).prop("checked", true);
    }
</script>
</body>
</html>
